import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/login'
import Layout from '../views/Layout'
Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },


  //首页
  {
    path: '/home', meta: { path: '/home', title: '首页' }, component: Layout, children: [
      { path: '', meta: { path: '', title: '首页' }, component: () => import('../views/home') }
    ]
  },

  // 用户
  {
    path: '/users', meta: { path: '/users', title: '用户管理' }, redirect: '/users/list', component: Layout, children: [
      { path: 'list', meta: { path: '/users/list', title: '用户列表' }, component: () => import('../views/users/list.vue') }
    ]
  },

  //权限
  {
    path: '/rights', meta: { path: '/rights', title: '权限管理' }, redirect: '/rights/peolist', component: Layout, children: [
      { path: 'roles', meta: { path: '/rights/roles', title: '角色列表' }, component: () => import('../views/Rights/peolist.vue') },
      { path: 'rights', meta: { path: '/rights/rights', title: '权限列表' }, component: () => import('../views/Rights/riglist.vue') }
 
    ]
  },

  //商品管理
  {
    path:'/goods',meta:{path:'/goods',title:'商品管理'},redirect:'/goods/list',component:Layout,children:[
      {path:'goods',meta:{path:'/goods/goods',title:'商品列表'},component:()=>import('../views/goods/list.vue')},
      {path:'params',meta:{path:'/goods/params',title:'分类参数'},component:()=>import('../views/goods/params.vue')},
      {path:'categories',meta:{path:'/goods/categories',title:'商品分类'},component:()=>import('../views/goods/categories.vue')},
      {path:'add',meta:{path:'/goods/add',title:'添加商品'},component:()=>import('../views/goods/addgoods.vue')},
    ]
  },

  // 订单管理
  {
    path: '/orders', meta: { path: '/orders', title: '订单管理' }, redirect: '/orders/list', component: Layout, children: [
      { path: 'list', meta: { path: '/order/list', title: '订单列表' }, component: () => import('../views/order/list.vue') }
    ]
  },
  // 数据统计
  {
    path: '/reports', meta: { path: '/reports', title: '数据统计' }, redirect: '/reports/list', component: Layout, children: [
      { path: 'list', meta: { path: '/reports/list', title: '数据报表' }, component: () => import('../views/reports/index.vue') }
    ]
  }
]

const router = new VueRouter({
  routes
})



// 导航守卫
router.beforeEach((to, from, next) => {
  console.log(to, from);
  if (to.path == '/login') {
    next()
  } else {
    if (sessionStorage.getItem('token_bd')) {
      next()
    } else {
      next('/login')
    }
  }
})



export default router
